<?php
use app\models\RbacManager;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 */

$this->title = '管理权限列表';
$this->params['breadcrumbs'][] = '权限管理';
$this->params['breadcrumbs'][] = $this->title;

app\assets\TableAsset::register($this);
app\assets\AjaxAsset::register($this);
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
    <div class="form-group">
        <a class="btn btn-success btn-sm" href="<?php echo Url::to(['/admin/rbac/edit-item']);?>">添加</a>
    </div>
<?php echo Html::endForm();?>
<table id="simple-table" class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th>上级权限</th>
            <th>下级权限</th>
            <th>操作</th>
        </tr>
    </thead>

    <tbody>
    <?php $top_item_list = RbacManager::getAuthItems();?>
    <?php foreach ($top_item_list as $top_item) {
        $sub_item_list = RbacManager::getAuthItems($top_item['name']);?>
        <tr>
            <th rowspan="<?php echo count($sub_item_list) + 1;?>"><?php echo $top_item['description'];?></th>
        </tr>
        <?php foreach ($sub_item_list as $sub_item) {?>
            <tr>
                <td><?php echo $sub_item['description'];?></td>
                <td><?php echo app\widgets\AdminTableOp::widget(['items'=>[
                        ['rbac'=>'rbac/item', 'icon'=>'fa fa-trash', 'onclick'=>'deleteItem(\'' . $sub_item['name'] . '\')', 'btn_class'=>'btn btn-xs btn-danger', 'tip'=>'删除', 'color'=>'red'],
                    ]]);?></td>
            </tr>
        <?php }?>
    <?php }?>
    </tbody>
</table>
<script>
    /**
     * 删除权限
     * @param name 权限名称
     */
    function deleteItem(name) {
        if (!confirm('确定要删除这个权限吗？')) {
            return false;
        }
        $.getJSON('<?php echo Url::to(['/admin/rbac/delete-item']);?>', {'name':name}, function(json) {
            if (callback(json)) {
                window.location.reload();
            }
        });
    }
</script>
